/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditDocumentPresenter.java
 * Date: 02/03/2011
 */

package com.ingenius.sau.client.presenter;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.ListDocumentsEvent;
import com.ingenius.sau.client.service.InstrumentsServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.core.SelfEvaluation;
import com.ingenius.sau.shared.model.instruments.Document;

/**
 * Class <strong>EditDocumentPresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditDocumentPresenter implements EditPresenter {

	public interface EditDocumentDisplay extends Display {

		String getName();

		void setName(String name);

		String getDescription();

		void setDescription(String description);

		String getProcessId();

		void setProcessId(String programId);

		void setProcesses(List<SelfEvaluation> processes);

		String getAttachmentUrl();

		void setAttachmentUrl(String url);

		String getAttachmentName();

		void setAttachmentName(String name);

		void show();

		void hide();

	}

	private final HandlerManager eventBus;
	private final InstrumentsServiceAsync rpcService;
	private final EditDocumentDisplay display;
	private Document document;

	public EditDocumentPresenter(HandlerManager eventBus,
			InstrumentsServiceAsync rpcService, EditDocumentDisplay display,
			String documentId) {

		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;
		loadProcesses();
		display.setPresenter(this);

		if (documentId == null) {
			document = new Document();

		} else {
			loadBean(documentId);
		}

	}

	private void loadProcesses() {
		rpcService.loadAllProcesses(new AsyncCallback<List<SelfEvaluation>>() {
			@Override
			public void onSuccess(List<SelfEvaluation> result) {
				display.setProcesses(result);
			}

			@Override
			public void onFailure(Throwable caught) {
				System.out.println("Error loading processes. "
						+ caught.getMessage());
			}
		});
	}

	private void loadBean(String documentId) {
		AppController.setStatusMessage("Loading...");
		rpcService.getDocument(documentId, new AsyncCallback<Document>() {
			@Override
			public void onSuccess(Document result) {
				if (result == null) {
					onFailure(new Throwable("Database error"));
					return;
				}
				document = result;
				display.setName(document.getName());
				display.setDescription(document.getDescription());
				display.setAttachmentUrl(document.getAttachmentUrl());
				display.setAttachmentName(document.getAttachmentName());
				display.setProcessId(document.getProcessId());
				hideStatusMessage();
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_GET + Document.class.getName(),
						caught);
				document = new Document();
				// TODO i18n
				String failedMessage = "Failed to retrieve document";
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.presenter.EditPresenter#doSaveOrUpdate()
	 */
	@Override
	public void doSaveOrUpdate() {
		document.setName(display.getName());
		document.setDescription(display.getDescription());
		document.setAttachmentUrl(display.getAttachmentUrl());
		document.setAttachmentName(display.getAttachmentName());
		document.setProcessId(display.getProcessId());

		// TODO i18n
		final String failedMessage = "Failed to save document";
		final String successMessage = "Documemt saved sucessfully";

		display.hide();
		AppController.setStatusMessage("Saving...");
		rpcService.saveOrUpdateDocument(document, new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					eventBus.fireEvent(new ListDocumentsEvent());
					display.showInfo(successMessage);
					hideStatusMessage();

				} else {
					onFailure(new Throwable("Database error"));
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_SAVE
						+ Document.class.getName(), caught);
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.presenter.Presenter#go(com.google.gwt.user.client
	 * .ui.HasWidgets)
	 */
	@Override
	public void go(HasWidgets container) {
		display.show();
	}

}
